package cn.lblbc.demo

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_lbl.*

/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》http://lblbc.cn/blog
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 */
class LblActivity : AppCompatActivity() {
    private val interval = 50L
    private val handler = Handler(Looper.getMainLooper())
    private val ballNumUtil = BallNumUtil()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lbl)
        startIv.setOnClickListener { startTimer() }
        pauseIv.setOnClickListener { pauseTimer() }
    }

    private fun startTimer() {
        handler.postDelayed(Runnable { changeBalls() }, interval)
        showPauseBtn()
    }

    private fun pauseTimer() {
        handler.removeCallbacksAndMessages(null)
        showStartBtn()
    }

    private fun changeBalls() {
        firstBallTv.text = ballNumUtil.getRedBall()
        secondBallTv.text = ballNumUtil.getRedBall()
        thirdBallTv.text = ballNumUtil.getRedBall()
        fourthBallTv.text = ballNumUtil.getRedBall()
        fifthBallTv.text = ballNumUtil.getRedBall()
        sixthBallTv.text = ballNumUtil.getRedBall()
        seventhBallTv.text = ballNumUtil.getBlueBall()
        handler.postDelayed({ changeBalls() }, interval)
    }

    private fun showStartBtn() {
        startIv.visibility = View.VISIBLE
        pauseIv.visibility = View.GONE
    }

    private fun showPauseBtn() {
        startIv.visibility = View.GONE
        pauseIv.visibility = View.VISIBLE
    }
}